<?php

class Upload extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		if ($_POST) 
		{ 
		    $uploadDir = "./assets/feeds/";
		    $RealTitleID = $_FILES['Filedata']['name'];
		
		    $ch = curl_init("http://amz.mws/api/feeds/submitFeed"); 
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		    curl_setopt($ch, CURLOPT_POST, 1);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		    $args['feed'] = new CurlFile($_FILES['Filedata']['tmp_name'],'file/exgpd',$RealTitleID);
		    $args['KEY']  = 'AC0TFQZ4SV2KM';
		    $args['FeedType']	  = '_POST_PRODUCT_DATA_';
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 
		    $result = curl_exec($ch);  

		    echo $result;die;     
		}

		$this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload()
	{
		

		echo "The file ".  basename( $_FILES['file']['name']). " has been uploaded";

		die;

		
	}

	function do_upload1()
	{
		$config['upload_path'] = './output/';
		$config['allowed_types'] = '*';
		$config['max_size']	= '0';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';

		$this->load->library('upload', $config);


		$this->upload->do_upload('userfile1');
	}


	function do_upload2()
	{
		$config['upload_path'] = './output/';
		$config['allowed_types'] = '*';
		$config['max_size']	= '0';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';

		$this->load->library('upload', $config);


		$this->upload->do_upload('userfile2');
	}
	
	
	function test()
	{
		$this->amazon_mws->create_product_feed('AC0TFQZ4SV2KM');
	}
	
	
	function feed( $type = null , $merchant_id = 'AC0TFQZ4SV2KM' )
	{
		
		if( is_null($type) )
			die('Please enter valid type of feed.');
		
		$feed = '';
		$feed_type = '';
		switch ($type)
		{
			case 'product':
				$feed = $this->amazon_mws->create_product_feed($merchant_id);
				$feed_type='_POST_PRODUCT_DATA_';
				break;
				
			case 'price':
				$feed = $this->amazon_mws->create_price_feed($merchant_id);
				$feed_type='_POST_PRODUCT_PRICING_DATA_';
				break;
				
			case 'image':
				$feed = $this->amazon_mws->create_image_feed($merchant_id);
				$feed_type='_POST_PRODUCT_IMAGE_DATA_';
				break;
				
			case 'inventory':
				$feed = $this->amazon_mws->create_inventory_feed($merchant_id);
				$feed_type='_POST_INVENTORY_AVAILABILITY_DATA_';
				break;
			default:
				
				break;
			
		}
		
		$fname = end(explode('/', $feed));
		//die($fname);
		$ch = curl_init("http://amz.mws/api/feeds/submitFeed");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$args['feed'] = new CurlFile($feed,'file/exgpd',$fname);
		$args['KEY']  		= $merchant_id;
		$args['FeedType']	= $feed_type;
		curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
		$result = curl_exec($ch);
		//echo $result;die;
		$result = json_decode(json_encode((array)simplexml_load_string($result)), true);
		//echo '<pre>';print($result);die;
		if( isset($result['GetReportRequestCountResult']) )
		{
			$result = $result['GetReportRequestCountResult'];
			$result = $result['SubmitFeedResult'];
			if( isset($result['FeedSubmissionInfo']) && is_array($result['FeedSubmissionInfo']) )
			{
				$records		= $result['FeedSubmissionInfo'];
				
				$insert = array();
				$insert['merchant_id'] = $merchant_id;
				$insert['feed_submission_id'] = $records['FeedSubmissionId'];
				$insert['status'] = $records['FeedProcessingStatus'];
					
				$this->db->insert('feeds', $insert);
				
				die('Feed has been sent sucessfully.');
			}
				
			
			
		}
		
		die('There is an error. Please try again.');
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
?>
